package org.gary.core.filter;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class LanguageFilter implements Filter {
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub		
		HttpServletRequest req=(HttpServletRequest)request;	
		if(req.getRequestURI().indexOf("image.action")>-1){		
			
		}else{
			HttpSession session = req.getSession();		
			if(session.getAttribute("WW_TRANS_I18N_LOCALE")==null){					
				String language=request.getLocale().toString();
				if("zh_CN".equalsIgnoreCase(language)){
					session.setAttribute("WW_TRANS_I18N_LOCALE", Locale.SIMPLIFIED_CHINESE);
				}else if("zh_TW".equalsIgnoreCase(language)){
					session.setAttribute("WW_TRANS_I18N_LOCALE", Locale.TRADITIONAL_CHINESE);
				}else if("en_US".equalsIgnoreCase(language)){
					session.setAttribute("WW_TRANS_I18N_LOCALE", Locale.US);
				}else{
					session.setAttribute("WW_TRANS_I18N_LOCALE", Locale.SIMPLIFIED_CHINESE);
				}
			}
		}
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}
}
